package com.jonluo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jonluo.exception.RReturnException;
import com.jonluo.model.SysDept;
import com.jonluo.service.SysDeptService;
import com.jonluo.utils.PageInfo;
import com.jonluo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@RestController
@RequestMapping(value="/dept")
@Api(description = "用户部门服务api")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation(value="获取用户部门列表", notes="分页查询用户部门",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"page\":1,\"limit\":10,\"params\":" +
            "{\"mobile\":\"123\",\"username\":\"ljw\"} }", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/getAll"}, method=RequestMethod.POST)
    public ResponseEntity getAll(@RequestBody JSONObject jsonObject){
            R r = null;
            PageInfo pageInfo =  new PageInfo(jsonObject);
            JSONObject json = new JSONObject();
            List<SysDept> list  = sysDeptService.queryList(pageInfo);
            Integer total = sysDeptService.queryTotal(pageInfo);
            json.put("total", total);
            json.put("data", list);
            r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="获取用户部门", notes="根据id查询用户部门",response = Map.class )
    @ApiImplicitParam(name = "id", value = "用户部门id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getById/{id}"}, method=RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id){
            R r = null;
            JSONObject json = new JSONObject();
            SysDept sysDept = sysDeptService.query(id);
            json.put("data", sysDept);
            r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="添加用户部门", notes="添加用户部门信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {对象实体}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/add"}, method=RequestMethod.POST)
    public ResponseEntity add(@RequestBody JSONObject jsonObject){
            R r = null;
            SysDept sysDept = JSON.parseObject(jsonObject.toJSONString(), SysDept.class);
            Integer sum = sysDeptService.save(sysDept);
            if (sum > 0){
                r = R.ok();
            }else {
                throw new RReturnException("添加用户部门失败");
            }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="修改用户部门", notes="根据id修改用户部门信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/update"}, method=RequestMethod.POST)
    public ResponseEntity update(@RequestBody JSONObject jsonObject){
            R r = null;
            SysDept sysDept = JSON.parseObject(jsonObject.toJSONString(), SysDept.class);
            Integer sum = sysDeptService.update(sysDept);
            if (sum > 0){
                r = R.ok();
            }else {
                throw new RReturnException("更新用户部门失败");
            }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="删除用户部门", notes="批量删除用户部门",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"ids\":\"id1,id2\"}多个id以逗号隔开", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/deleteByIds"}, method=RequestMethod.POST)
    public ResponseEntity deleteByIds(@RequestBody JSONObject jsonObject){
        R r = null;
        String[] ids= jsonObject.getString("ids").split(",");
            Integer sum = sysDeptService.deleteBatch(ids);
            if (sum == ids.length){
                r = R.ok();
            }else {
                throw new RReturnException("批量删除用户部门失败");
            }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value="获取下属部门列表", notes="获取下属部门列表信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"parentId\":1\",\"isAll\":true}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/getSubDetpList"}, method=RequestMethod.POST)
    public ResponseEntity getSubDetpList(@RequestBody JSONObject jsonObject){
        R r = null;
        JSONObject json = new JSONObject();
        List<SysDept> list = null;
        if (jsonObject.getBoolean("isAll")){
            list = sysDeptService.queryAllSubDeptList(jsonObject.getString("parentId"));
        }else {
            list = sysDeptService.querySubDeptList(jsonObject.getString("parentId"));
        }

        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

}
